package com.smartcar

import android.content.Context
import android.net.wifi.WifiManager
import android.util.Log

class IpGetUtils(var context: Context){
    private val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    fun isWifiOpened(): Boolean {
        return wifiManager.isWifiEnabled();
    }
    /**
     * 获取当前ip地址
     *
     * @param context
     * @return
     */
    fun getLocalIpAddress(): String {
        return try {
            //获取wifi服务
            val wifiInfo = wifiManager.connectionInfo
            val i = wifiInfo.ipAddress
            int2ip(i)
        } catch (e: Exception) {
            Log.d("log", "getLocalIpAddress")
            ""
        }
    }
    /**
     * 将ip的整数形式转换成ip形式
     *
     * @param ipInt
     * @return
     */
    private fun int2ip(ipInt: Int): String {
        val sb = StringBuilder()
        sb.append(ipInt and 0xFF).append(".")
        sb.append(ipInt shr 8 and 0xFF).append(".")
        sb.append(ipInt shr 16 and 0xFF).append(".")
        sb.append(ipInt shr 24 and 0xFF)
        return sb.toString()
    }

}
